var path = require("path");
var fs = require("fs");
var webpack = require('webpack');
var node_modules_dir = path.resolve(__dirname, 'node_modules');
var htmlWebpackPlugin = require("html-webpack-plugin");
var baseConfig = require("./webpack.base.config");
var pages = require("./pages")
// 获取指定路径下的入口文件
function getEntries(globPath) {
    var entries = {};
    pages.forEach(function (page) {
        page.active && (entries[page.name] = './src/entry/'+page.name);
    })
    return entries;
}

var entries =  getEntries('src/entry')
// baseConfig.devtool = '#eval-source-map'
baseConfig.devtool = '#cheap-module-source-map'

baseConfig.plugins.push(
    new webpack.optimize.CommonsChunkPlugin(
        {
            name:'lib',
            filename:'react.js'
        }
    ),
    new webpack.DefinePlugin({
        'process.env.NODE_ENV': JSON.stringify('development')
    })
)
baseConfig.entry.lib = ['react','react-dom'];

Object.keys(entries).forEach(function(name) {
    // 每个页面生成一个entry，如果需要HotUpdate，在这里修改entry
    baseConfig.entry[name] = ['whatwg-fetch','babel-polyfill',entries[name]];

    // 每个页面生成一个html
    var plugin = new htmlWebpackPlugin({
        // 生成出来的html文件名
        filename: name + '.html',
        // 每个html的模版，这里多个页面使用同一个模版
        template: './app.html',
        // 自动将引用插入html
        inject: true,
        // 每个html引用的js模块，也可以在这里加上vendor等公用模块
        chunks: [name, 'lib'],
    });
    baseConfig.plugins.push(plugin);
})

module.exports = baseConfig